package org.blame.workQueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.blame.constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
//        1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL);

        Connection connection= connectionFactory.newConnection();

//        2.开启信道
        Channel channel=connection.createChannel();
//        申明队列，使用的是内置的交换机
//        如果队列不存在，则创建队列，否则则不创建
        channel.queueDeclare(Constants.QUEUE_NAME,true,false,false,null);
//        进行消息的发送
        for (int i = 0; i < 10; i++) {
            String msg = "Hello World" + i;

            channel.basicPublish("",Constants.QUEUE_NAME,null,msg.getBytes());
        }
        System.out.println("消息发送成功");
//        channel.close();
//        connection.close();
    }
}
